<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>首页</title>
    <link rel="stylesheet" href="/asset/layui/css/layui.css">
    <link rel="stylesheet" href="/asset/css/index.css">
    <link rel="stylesheet" href="/asset/plugin/formSelects/dist/formSelects-v4.css">
    <script type="text/javascript" src="/asset/js/jquery-3.3.1.min.js"></script>
	<script src="/asset/layui/layui.js"></script>
</head>
<body>

	<div class="alert_bigbox">
		
		<form class="layui-form">
		    
		    <div class="layui-form-item">
			    <label class="layui-form-label">搜索公司</label>
			    <div class="layui-input-inline" style="width:400px">
			         <select name="companyId" xm-select="company" xm-select-search="" xm-select-radio="">
				        <option value="">输入公司名称搜索</option>
				    </select>
			    </div>
			</div>
			
			<div class="layui-form-item">
              <label class="layui-form-label"></label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">确认分配</button>
          </div>
		    
		</form>
	
	</div>
	
<script>


	layui.config({
        base: '/asset/plugin/formSelects/dist/'
    }).extend({
        formSelects: 'formSelects-v4'
    });
    
    layui.use(['form','formSelects'], function(){
        
        var $ = layui.jquery
            ,form = layui.form
            ,layer= layui.layer
            ,formSelects = layui.formSelects;
            
        formSelects.config('company', {
	            searchUrl: '/admin/company/search.htm'
	    });
        
        
        
        form.on('submit(add)', function(data){
        		
        		var field = data.field ;
        		
        		if(!field.companyId){
        			layer.msg('请选择公司');
        		}
        		
        		var $checked  = parent.$("input:checkbox[name='devicesId']:checked");
        		if($checked.length == 0){
	  			layer.msg('请选择设备');
	  		}else{
	  			
	  			$checked.each(function(i,item){
	  				var devicesId = $(this).val();
	  				field["ids["+i+"]"] = devicesId;
	  			}) 
	  		}
        		
        		
        
        
        		console.log(field);
        		
        		$.ajax({
      			type:"POST", 
      			url:"/admin/devices/alloacation.htm",
      			data:data.field,
      			success:function(result) {
		      
		      		if(result.code=='1'){
		      			layer.alert("增加成功", {icon: 6},function () {
			                // 获得frame索引
			                 window.parent.location.reload();
			                var index = parent.layer.getFrameIndex(window.name);
			                //关闭当前frame
			                parent.layer.close(index);
			            });
		      		}else{
		      		 	layer.msg(result.msg);
		      		}
		      
		    	 	} 
		    }); 
            
            
            return false;
          });
        
         
    });





</script>
</body>
</html>